package dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import bean.Admin;
import bean.DBHelper;
import bean.Question;
import bean.Student;

public class AdminDao {
	public boolean checkID(String ID,String pwd)
	{
		boolean sign=false;
		String sql="select apwd from admin where aid="+ID;
		ResultSet rs=DBHelper.executeQuery(sql);
		try{
			while(rs.next()){
				if(pwd.equals(rs.getString(1)))
				{
					sign=true;
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		DBHelper.close();
		return sign;
	}
	public String getAdminName(String id){
		String name=null;
		String sql="select aname from admin where aid="+id;
		ResultSet rs=DBHelper.executeQuery(sql);
		try{
			while(rs.next()){
				name=rs.getString(1);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		DBHelper.close();
		return name;
	}
	public boolean addAdmin(Admin a){
		String sql=null;
		boolean sign=false;
		try{
			sql="insert into admin values('"+a.getAid()+"','"+a.getAname()+"','"+a.getApwd()+"')";
			DBHelper.executeUpdate(sql);
			sign=true;
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		DBHelper.close();
		return sign;
	}
	public boolean deleteAdminByAid(String aid){
		String sql=null;
		try{
			sql="delete from admin where aid='"+aid+"'";
			DBHelper.executeUpdate(sql);
			return false;		
		}catch(Exception ex){
			ex.printStackTrace();
		}
		DBHelper.close();
		return false;
	}
	public List<Admin> allAdmin() {
		List<Admin> list = new ArrayList<Admin>();
		String sql = "select * from admin";
		ResultSet rs = DBHelper.executeQuery(sql);
        try{
		while (rs.next()) {
			Admin ad = new Admin();
				ad.setAid(rs.getString(1));
				ad.setAname(rs.getString(2));
				ad.setApwd(rs.getString(3));				
				list.add(ad);
			}
        } 
        catch (Exception ex) 
        {
			ex.printStackTrace();
		}
        DBHelper.close();
		return list;
	}
	public ArrayList<Admin> getOnePage(int position,int pageSize) {
		ArrayList<Admin> list = new ArrayList<Admin>();
		String sql = "select * from admin";
		ResultSet rs = DBHelper.executeQuery(sql);
		 try{
			 
				for(int i=0;i<position;i++){
					rs.next();
				}
				for(int j=0;j<pageSize;j++){
					Admin ad = new Admin();
					ad.setAid(rs.getString(1));
					ad.setAname(rs.getString(2));
					ad.setApwd(rs.getString(3));				
					list.add(ad);
					rs.next();
				}
		     } 
		        catch (Exception ex) 
		        {
					ex.printStackTrace();
				}
		        DBHelper.close();
				return list;
	}
	public ArrayList<Admin> getLastPage(int position) {
		ArrayList<Admin> list = new ArrayList<Admin>();
		String sql = "select * from admin";
		ResultSet rs = DBHelper.executeQuery(sql);
		 try{
			 
				for(int i=0;i<position;i++){
					rs.next();
				}
				do{
					Admin ad = new Admin();
					ad.setAid(rs.getString(1));
					ad.setAname(rs.getString(2));
					ad.setApwd(rs.getString(3));				
					list.add(ad);
					}while (rs.next()) ;
		        } 
		        catch (Exception ex) 
		        {
					ex.printStackTrace();
				}
		        DBHelper.close();
				return list;
	}
}
